﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class ProductComments : BasePage
    {
        int ProductId = 0;
        int CurrentPage = 1;
        protected void Page_Load( object sender, EventArgs e )
        {
            int.TryParse(Request.QueryString["id"], out ProductId);
            if (!int.TryParse(Request.QueryString["page"], out CurrentPage))
            {
                CurrentPage = 1;
            }
            hidProductId.Value = ProductId.ToString();
            hidPage.Value = CurrentPage.ToString();

            AjaxProcess();
        }

        protected void AjaxProcess( )
        {
            if (Request.QueryString["sync"] == "true")
            {
                Response.Write(GetListJson());
                Response.End();
            }
        }

        protected string GetListJson( )
        {
            BLL.Comments coBLL = new BLL.Comments();
            string strWhere = "productId=" + ProductId + " and IsActive=1 and ShowStatus=1";
            string strOrder = "CommentId desc";
            int recordCount = coBLL.GetRecordCount(strWhere);
            int startIndex = 0;
            int endIndex = 0;
            int total = 0;
            if (recordCount <= 10)
            {
                total = 1;
                startIndex = 1;
                endIndex = recordCount;
            }
            else
            {
                total = recordCount % 10 == 0 ? recordCount / 10 : recordCount / 10 + 1;
                startIndex = 10 * (CurrentPage - 1) + 1;
                endIndex = 10 * CurrentPage;
            }
            DataSet ds = coBLL.GetListByPageV(strWhere, strOrder, startIndex, endIndex);
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                if (strBuilder.ToString() != "")
                {
                    strBuilder.Append(",");
                }
                strBuilder.Append("{\"id\":\"" + dr["ProductId"] + "\",\"nickname\":\"" + dr["nickname"] +
                    "\",\"date\":\"" + Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd") + "\",\"point\":\"" + dr["point"] + "\",\"detail\":\"" + dr["Detail"] + "\"}");
            }

            return "{\"total\":\"" + recordCount + "\",\"currPage\":\"" + CurrentPage + "\",\"list\":[" + strBuilder.ToString() + "]}";
        }
    }
}